<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/11 0011
 * Time: 15:00
 */

namespace app\currency\controller;


use app\currency\service\AgentService;
use app\currency\service\CitiesService;
use app\currency\service\CleanerService;
use library\Controller;
use library\service\NodeService;
use think\Db;

/**
 * 保洁员管理
 * Class Cleaner
 * @package app\currency\controller
 */
class Cleaner extends Controller
{
    protected $table = 'Cleaner';

    /**
     * 保洁员列表
     * @menu true
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '保洁员信息管理';
        $where = [];
        $id = $this->request->get('id');
        if(!empty($id)){
            $this->title = '团队列表';
            $ids = AgentService::getTeamIds($id);
            if(!empty($ids))
            {
                $where[] = ['id','in',$ids];
            } else {
                $where[] = ['id','eq','0'];
            }
        }
        $query = $this->_query($this->table)->where($where)->like('name,phone');
        $query->equal('status,work_state')
            ->dateBetween('create_at')
            ->order('id desc')
            ->page();
    }
    protected function _index_page_filter(array &$data)
    {
        foreach ($data as &$vo) {
            $vo['bind_bank_status'] = (boolean)Db::name('CleanerBankCard')->where('cid',$vo['id'])->count();
        }
    }
    /**
     * 添加保洁员
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑保洁员信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->applyCsrfToken();
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     */
    public function _form_filter(&$data)
    {
        if ($this->request->isPost()) {
            if (isset($data['id'])) {
                unset($data['phone']);
            } elseif (Db::name($this->table)->where(['phone' => $data['phone']])->count() > 0) {
                $this->error('手机号已经存在，请使用其它手机号！');
            }
        }
    }

    /**
     * 禁用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 删除
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }

    public function choose_position()
    {
        $this->fetch();
    }
    public function relation()
    {
        $id = $this->request->get('id');
        $member = Db::name($this->table)->where('id',$id)->find();
        $parent_member = [];
        if($member['pid'] != 0){
            $parent_member = Db::name($this->table)->where('id',$member['pid'])->find();
        }
        $p_member = [];
        if(!empty($parent_member) && $parent_member['pid'] != 0){
            $p_member = Db::name($this->table)->where('id',$parent_member['pid'])->find();
        }
        $agent_id = CleanerService::getAgent($id);
        $agent = [];
        if($agent_id > 0){
            $agent = Db::name('Agent')->where('id',$agent_id)->find();
        }
        $this->assign([
            'member' => $member,
            'parent' => $parent_member,
            'grand' => $p_member,
            'agent' => $agent
        ]);
        $this->fetch();
    }
    public function customer(){
        $this->title = '绑定地址列表';
        $id = $this->request->get('id');
        $where = [['a.cid','eq',$id]];
        $this->_query(Db::name('CitiesCleaner')->alias('a')
            ->leftJoin('cities b', 'a.cities_id=b.id')
            ->where($where)
            ->field('b.*,a.create_at')
        )->where($where)->page();
    }
    protected function _customer_page_filter(array &$data){
        foreach ($data as &$datum) {
            $datum['address_txt'] = CitiesService::getAddressText($datum['id'],'string');
        }
    }
    public function rebate(){
        $this->title = '收益记录';
        $id = $this->request->get('id');
        $where = [['cid','eq',$id]];
        $this->_query('CleanOrderRoyalty')->where($where)->order('create_at desc')->page();
    }
    protected function _rebate_page_filter(array &$data)
    {
        $cids = array_unique(array_column($data, 'ccid'));
        $cleaner_list = Db::name('Cleaner')->whereIn('id', $cids)->select();
        foreach ($data as &$datum) {
            $datum['cleaner'] = [];
            foreach ($cleaner_list as $cleaner) if($cleaner['id'] === $datum['ccid']) {
                $datum['cleaner'] = $cleaner;
            }
        }
//        dump($data);
    }
    public function balance_log()
    {
        $this->title = '余额记录';
        $id = $this->request->get('id');
        $where = [['cid','eq',$id]];
        $this->_query('CleanerBalanceLog')->where($where)->order('create_at desc')->page();
    }
}